<?php
$host = 'localhost';  // 数据库主机名
$username = "root";   // 数据库用户名
$password = "root";  // 数据库密码
$dbname = "test";    // 数据库名
$connID = mysqli_connect($host, $username, $password, $dbname);  // 建立数据库连接
if (mysqli_select_db($connID, $dbname)) {    // 选择数据库
    echo "数据库连接成功"; // 数据库连接成功
    $sql = "CREATE TABLE `user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '名称',
  `age` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";

//    if (mysqli_query($connID, $sql) === TRUE) {
//        echo "表创建成功"; // 表创建成功
//    }
    // 查询
    $sql = "SELECT * FROM user";
    $result = mysqli_query($connID, $sql);

    if (mysqli_num_rows($result) > 0) {
        // 输出数据
        while($row = mysqli_fetch_assoc($result)) {
            echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. "<br>";
        }
    }



} else {
    echo "数据库连接失败"; // 数据库连接失败
}
?>